#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

#define FOR(i,a,n) for(int i=a;i<n;++i)
#define REP(i,n) FOR(i,0,n)

using namespace std;
class SuffixArray
{
public:
    int *sa,*rank;
    int *height;
    int (*rmq)[20];
    int keyTypeMax,length;
    SuffixArray(int *a,int n,int m)
    {
        keyTypeMax = max(m,n)+5;
        sa = new int[n+10];
        rank = new int[keyTypeMax];
        height = new int[n+10];
        length = n+1;
        a[n]=0;
        setSa(a);
        setHeight(a);
        setLcpRmq();
    }
    ~SuffixArray()
    {
        delete(sa);
        delete(rank);
        delete(height);
        delete(rmq);
    }
    void setSa(int *a)
    {
        int *cnt = new int[keyTypeMax];
        REP(i,keyTypeMax) cnt[i] = 0;
        REP(i,length) cnt[rank[i]=a[i]]++;
        REP(i,keyTypeMax-1) cnt[i+1]+=cnt[i];
        for(int i=length-1; i>=0; --i) sa[--cnt[rank[i]]] = i;

        int *rankSt = new int[keyTypeMax];
        int *rankStPos = new int[length+10];
        for(int p=0,h=1,rankMax=keyTypeMax;
                p<length; h<<=1,rankMax=p)
        {
            p = 0;
            FOR(i,length-h,length) rankStPos[p++]=i;
            REP(i,length) if(sa[i]>=h) rankStPos[p++] = sa[i]-h;
            REP(i,length) rankSt[i]= rank[rankStPos[i]];
            REP(i,rankMax) cnt[i] =0;
            REP(i,length) cnt[rankSt[i]]++;
            REP(i,rankMax-1) cnt[i+1]+=cnt[i];
            for(int i = length-1; i>=0; i--) sa[--cnt[rankSt[i]]]=rankStPos[i];

            swap(rank,rankSt);
            rank[sa[0]] = 0;
            p=1;
            FOR(i,1,length)
            {
                if(rankSt[sa[i]]==rankSt[sa[i-1]]
                        &&rankSt[sa[i]+h]==rankSt[sa[i-1]+h])
                    rank[sa[i]] = p-1;
                else rank[sa[i]] = p++;
            }
        }
        delete(rankStPos);
        delete(rankSt);
        delete(cnt);
    }
    void setHeight(int *a)
    {
        int h = 0;
        REP(i,length)
        {
            int  r = rank[i];
            if(!r) height[r] = h =0;
            else
            {
                int pb = sa[r-1];
                int off=(h?h-1:0);
                while(a[i+off]==a[pb+off])off++;
                height[r] = h =off;
            }
        }
    }
    void setLcpRmq()
    {
        rmq = new int[length+10][20];
        REP(i,length) rmq[i][0] = height[i];
        for(int k = 0;(1<<k)<length;k++)
        {
            int len = 1<<k;
            FOR(i,length-len,length) rmq[i][k+1] = rmq[i][k];
            REP(i,length-len) rmq[i][k+1] = min(rmq[i][k],rmq[i+len][k]);
        }
    }
    int getLcp(int i,int j)
    {

        if(i==j) return length-i-1;
        i = rank[i],j = rank[j];
        if(j<i) swap(i,j);
        int k = 0,len=0;
        for(int st=i+1;st+(1<<k)-1<=j;k++) len = 1<<k;
        return min(rmq[i+1][k-1],rmq[j-len+1][k-1]);
    }
};

int a[50500],length,slen;
int toNewPos[50500];
int nearInsertedDist[50500];
int oldCharPos[50500];
char s[50500];
void iDemand()
{
    char c[2];
    int pos;
    scanf("%s%d",c,&pos);
    pos--;
    if(pos>slen) pos = slen;
    for(int i = slen;i>pos;i--) s[i] = s[i-1],oldCharPos[i] = oldCharPos[i-1];
    slen++;
    s[pos] = c[0]; oldCharPos[pos] = -1;
    int tpos = lower_bound(toNewPos,toNewPos+length,pos)-toNewPos;
    FOR(i,tpos,length) toNewPos[i]++;
    for(int i = tpos-1;i>=0&&nearInsertedDist[i]>pos-toNewPos[i];i--)
        nearInsertedDist[i]=pos-toNewPos[i];
}

int qDemand(SuffixArray &sa,int lb,int rb)
{
    int lcp = sa.getLcp(lb,rb);
    int len1 = nearInsertedDist[lb];
    int len2 = nearInsertedDist[rb];
    int len = min(len1,len2);
    if(lcp<len) return lcp;
    else
    {
        lcp = len;
        int pa = toNewPos[lb]+len,pb = toNewPos[rb]+len;
        while(pb<slen&&pa<slen)
        {
            int opa = oldCharPos[pa],opb = oldCharPos[pb];
            if(opa!=-1&&opb!=-1)
            {
                lcp+=qDemand(sa,opa,opb);
                break;
            }
            else
            {
                if(s[pa++]==s[pb++]) lcp++;
                else break;
            }
        }
        return lcp;
    }
}
int main()
{
    int n;
    scanf("%s%d",s,&n);
    slen = length = strlen(s);
    REP(i,length)
    {
        a[i] = s[i];
        toNewPos[i] = i;
        nearInsertedDist[i] = 1<<29;
        oldCharPos[i] = i;
    }
    SuffixArray sa(a,length,256);
    REP(i,n)
    {
        char q[2];
        scanf("%s",q);
        if(q[0]=='I')
        {
            iDemand();
        }
        else
        {
            int lb,rb;
            scanf("%d%d",&lb,&rb);
            lb--,rb--;
            printf("%d\n",qDemand(sa,lb,rb));
        }
    }

	return 0;
}
